package top.fangw.tenant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import top.fangw.common.mybatis.annotation.DyDataSource;
import top.fangw.common.mybatis.service.BaseTreeService;
import top.fangw.tenant.mapper.ApplicationInfoMapper;
import top.fangw.tenant.pojo.dto.MenuInfoQueryDto;
import top.fangw.tenant.pojo.entity.ApplicationInfo;
import top.fangw.tenant.pojo.entity.MenuInfo;
import top.fangw.tenant.mapper.MenuInfoMapper;
import top.fangw.tenant.pojo.vo.SysMenuInfoVO;
import top.fangw.tenant.service.IMenuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用途：用于管理系统中的菜单信息 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-02-18
 */
@Service
@RequiredArgsConstructor
public class MenuInfoServiceImpl extends ServiceImpl<MenuInfoMapper, MenuInfo> implements IMenuInfoService {

    private final MenuInfoMapper menuInfoMapper;

    @Override
    @DyDataSource
    public void add(MenuInfo menuInfo) {
        BaseTreeService<MenuInfoMapper, MenuInfo> treeService = new BaseTreeService<>(menuInfoMapper);
        treeService.saveOrUpdateTree(menuInfo);
    }

    @Override
    public void delete(String id) {
        //判断其是否有子菜单，如果有则不能够删除
        List<MenuInfo> childrenMenuList = menuInfoMapper.selChildrenByParentId(id);
        if (!CollectionUtils.isEmpty(childrenMenuList)) {
            throw new RuntimeException("该菜单下有子菜单，不能够删除");
        } else {
            //删除菜单信息
            menuInfoMapper.deleteById(id);
        }
    }

    @Override
    public List<MenuInfo> treeList() {
        LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<MenuInfo>().orderByAsc(MenuInfo::getSort).eq(MenuInfo::getIsShow, "1");
        BaseTreeService<MenuInfoMapper, MenuInfo> treeService = new BaseTreeService<>(menuInfoMapper, new MenuInfo());
        return treeService.treeList(queryWrapper);
    }

    @Override
    public IPage<MenuInfo> getListPage(MenuInfoQueryDto menuInfoQueryDto) {
        LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<MenuInfo>().orderByAsc(MenuInfo::getSort);
        if (StrUtil.isNotBlank(menuInfoQueryDto.getName())) {
            queryWrapper.like(MenuInfo::getName, menuInfoQueryDto.getName());
        }
        if (StrUtil.isNotBlank(menuInfoQueryDto.getApplicationId())) {
            queryWrapper.eq(MenuInfo::getApplicationId, menuInfoQueryDto.getApplicationId());
        }
        BaseTreeService<MenuInfoMapper, MenuInfo> treeService = new BaseTreeService<>(menuInfoMapper, new MenuInfo());
        return treeService.pageList(queryWrapper, menuInfoQueryDto.getCurrent(), menuInfoQueryDto.getSize());
    }


}
